<?php

namespace App\Http\Controllers\Back;

use App\Http\Service\ServiceBook;
use App\Http\Service\ServiceLtBook;
use App\Model\Book;
use App\Model\Picture;
use App\Util\Result;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

class XjBookController extends Controller
{
    use ServiceLtBook;
    private function leftBar() {
        return [
            'title' => '图书管理',
            'list'  => [
                [
                    'title' => '书籍仓库',
                    'type' => 'book_list',
                    'url' => url('back/book'),
                ],
                [
                    'title' => '待上架书籍',
                    'type' => 'collect',
                    'url' => url('back/book/collect'),
                ],
                [
                    'title' => '在架书籍',
                    'type' => 'sjBook',
                    'url' => url('back/sjBook/index'),
                ],
                [
                    'title' => '外借图书',
                    'type' => 'ltBook',
                    'url' => url('back/ltBook/index'),
                ],
                [
                    'title' => '下架书籍',
                    'type' => 'xjBook',
                    'url' => url('back/xjBook/index'),
                ],
                [
                    'title' => '外售图书',
                    'type' => 'wsBook',
                    'url' => url('back/wsBook/index'),
                ],
                [
                    'title' => '异常书籍',
                    'type' => 'ycBook',
                    'url' => url('back/ycBook/index'),
                ],
                [
                    'title' => '流通日志',
                    'type' => 'ltlog',
                    'url' => url('back/ltlog/index'),
                ],
            ]
        ];
    }

    /**
     * @param Request $request
     * czs
     */
    public function __construct(Request $request){
        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * 图书列表
     * czs
     */
    public function index() {
        $keyWord = $this->request->input('keyword');
        $res = self::getXjLists($keyWord,$this->limit)->getData();
        $this->request->flash();
        return view('back/xjBook/index')
            ->with('res',$res['list'])
            ->with('count',$res['count'])
            ->with('count2',$res['count2'])
            ->with('titleType','xjBook');
    }

    public function edit($id) {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $pic = DB::table('ttindex7')->where('ID',$id)->select('ImageUrl  as img')->first();
            if (empty($input['pic_url_only'])&&empty($pic->img)) {
                return Result::failure()->setMsg("图片不能为空")->getResult();
            }
            //单图更新
            if ($pic->img!=$input['pic_url_only']) {
                $input['ImageUrl'] = $input['pic_url_only'];
                unset($input['pic_url_only']);
            } else {
                unset($input['pic_url_only']);
            }
            $res = DB::table('ttindex7')->where('ID',$id)->update($input);
            if ($res) {
                return Result::success()->getResult();
            } else {
                return Result::failure()->getResult();
            }
        } else {
            $res = DB::table('ttindex7 as a')
                ->where('a.ID',$id)
                ->select('a.Title','a.ImageUrl')
                ->first();
            return view('back.xjBook.edit')
                ->with('res',$res)
                ->with('id',$id)->with('titleType','xjBook');
        }
    }

    /**
     * 二级典藏
     */
    public function secXj($id) {
        $res = self::getSecXj($id,$this->limit)->getData();
        return view('back/xjBook/secXj')
            ->with('res',$res['list'])
            ->with('count',$res['count'])
            ->with('count2',$res['count2'])
            ->with('id',$id)->with('titleType','xjBook');
    }

    public function editSecXj($id) {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $pic = self::getPic($id)->getData();
            if (empty($input['pic_url_only'])&&empty($pic->img)) {
                return Result::failure()->setMsg("图片不能为空")->getResult();
            }
            //单图更新
            if ($pic->img!=$input['pic_url_only']) {
                $input['ImageUrl'] = $input['pic_url_only'];
                unset($input['pic_url_only']);
            } else {
                unset($input['pic_url_only']);
            }

            $res = self::updateData($input, $id);
            return $res->getResult();
        } else {
            $res = DB::table('circ_marc as a')
                ->where('a.MarcID',$id)
                ->select('a.Title','a.ImageUrl','a.Price','a.IID','a.Profile')
                ->first();
            return view('back.xjBook.editSecXj')
                ->with('res',$res)
                ->with('id',$id)->with('titleType','xjBook');
        }
    }

    public function dcThirdXj($id) {
        $sel = DB::table('Items')
            ->where('SerialNo',$id)
            ->where('LTStatus',5);
        if (empty($sel->count())) {
            return Result::failure()->getResult();
        }
        $sel2 = DB::table('Items as a')
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->where('a.SerialNo',$id)
            ->select('b.ImageUrl','a.sold_price','a.rent_price','b.Profile')
            ->first();
        if (empty($sel2->ImageUrl)||empty($sel2->sold_price)||empty($sel2->rent_price)||empty($sel2->Profile)) {
            return Result::failure()->setMsg('图片、售价、租金、简介不能为空！')->getResult();
        }
        $res = $sel->update(['LTStatus'=>2]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    public function hkThirdXj($id) {
        //更改书籍状态
        $res = DB::table('Items')->where('SerialNo',$id)->update(['LTStatus'=>1]);
        if(empty($res)){
            return Result::failure()->setType(1)->getResult();
        }
        return Result::success()->getResult();
    }

    public function plhkXj() {
        $ids = $this->request->input('ids');
        $res = DB::table('Items as a')
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID')
            ->wherein('c.ID',$ids)->update(['LTStatus'=>1]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    public function plhkSecXj() {
        $ids = $this->request->input('ids');
        $res = DB::table('Items as a')
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->wherein('b.MarcID',$ids)
            ->update(['LTStatus'=>1]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    public function plhkThirdXj() {
        $ids = $this->request->input('ids');
        //更改书籍状态
        $res = DB::table('Items')->wherein('SerialNo',$ids)->update(['LTStatus'=>1]);
        if(empty($res)){
            return Result::failure()->setType(1)->getResult();
        }
        return Result::success()->getResult();
    }

    public function pldcXj() {
        $ids = $this->request->input('ids');
        $res = DB::table('Items as a')
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->leftjoin('ttindex7 as c','c.ID','=','b.IID')
            ->wherein('c.ID',$ids)->update(['LTStatus'=>1]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    public function pldcSecXj() {
        $ids = $this->request->input('ids');
        $res = DB::table('Items as a')
            ->leftjoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->wherein('b.MarcID',$ids)
            ->update(['LTStatus'=>1]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    public function pldcThirdXj() {
        $ids = $this->request->input('ids');
        //更改书籍状态
        $res = DB::table('Items')->wherein('SerialNo',$ids)->update(['LTStatus'=>2]);
        if(empty($res)){
            return Result::failure()->setType(1)->getResult();
        }
        return Result::success()->getResult();
    }

    public function editSecLt($id) {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $pic = self::getPic($id)->getData();
            if (empty($input['pic_url_only'])&&empty($pic->img)) {
                return Result::failure()->setMsg("图片不能为空")->getResult();
            }
            //单图更新
            if ($pic->img!=$input['pic_url_only']) {
                $input['ImageUrl'] = $input['pic_url_only'];
                unset($input['pic_url_only']);
            } else {
                unset($input['pic_url_only']);
            }

            $res = self::updateData($input, $id);
            return $res->getResult();
        } else {
            $res = DB::table('circ_marc as a')
                ->where('a.MarcID',$id)
                ->select('a.Title','a.ImageUrl','a.Price','a.IID','a.Profile')
                ->first();
            return view('back.ltBook.editSecLt')
                ->with('res',$res)
                ->with('id',$id)->with('titleType','ltBook');
        }
    }

    /**
     * 三级典藏
     */
    public function thirdXj($id) {
        $iid = DB::table('circ_marc as a')
            ->where('a.MarcID',$id)->value('a.IID');
        $res = self::getThirdXj($id,$this->limit)->getData();
        return view('back/xjBook/thirdXj')
            ->with('res',$res['list'])
            ->with('count',$res['count'])
            ->with('id',$id)->with('titleType','xjBook')->with('iid',$iid);
    }

    public function editThirdLt($id) {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $res = self::updateThirdCata($input, $id);
            return $res->getResult();
        } else {
            $res = DB::table('Items as a')
                ->where('a.SerialNo',$id)
                ->select('a.sold_price','a.rent_price','a.MarcID')
                ->first();
            return view('back.ltBook.editThirdLt')
                ->with('res',$res)
                ->with('id',$id)->with('titleType','ltBook');
        }
    }

    public function ghThirdLt($id) {
        DB::beginTransaction();
        $rfid = DB::table('TagInfo as a')
            ->leftjoin('Items as b','b.BarCode','=','a.Barcode')
            ->where('b.SerialNo', $id)
            ->value('a.rUID');
        if (empty($rfid)) {
            DB::rollback();
            return Result::failure()->setType(1)->getResult();
        }
        //更改读者书状态
        $i['status'] = 1;
        $i['created_at'] = time();
        $find = DB::table('member_books')
            ->where('bid', $rfid);
        if ($find->count() > 0) {
            $res1 = $find->update($i);
            if (empty($res1)) {
                DB::rollback();
                return Result::failure()->setType(2)->getResult();
            }
        }
        //更改门书状态
        $y['created_at'] = time();
        $y['status'] = 1;
        $doorBook = DB::table('door_books')->where('rfid', $rfid);
        if ($doorBook->count() > 0) {
            $res2 = $doorBook->update($y);
            if (empty($res2)) {
                DB::rollback();
                return Result::failure()->setType(3)->getResult();
            }
        }
        //更改图书状态
        $sel = DB::table('Items')
            ->where('SerialNo',$id)
            ->update(['LTStatus'=>3]);
        if (empty($sel)) {
            DB::rollback();
            return Result::failure()->setType(4)->getResult();
        }
        DB::commit();
        return Result::success()->getResult();
    }
}
